#
# Copyright 2008-2010 Vadim Zhukov <persgray@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AGAVA COMPANY ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AGAVA COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the AGAVA Company.

package RB::Log;

use bytes;
use strict;

use Exporter qw/import/;
use IO::Handle;
use POSIX qw/strftime/;

our @EXPORT = qw/errx logIt genTimestamp openGlobalLog/;


# Returns:
#   date (timestamp) string used in archive information files
sub genTimestamp() {
	return strftime('%Y-%m-%d %H:%M:%S', localtime);
}

# Returns:
#   boolean (true if no error, false if error occured)
#
# Side effects:
#   open file handle "LOG"
sub openGlobalLog() {
	my $rc = open LOG, '>>', ${main::Config}->{'archive_dir'}.'/rb.log';
	if ($rc) {
		LOG->autoflush(1);
	} else {
		print STDERR "WARNING: Cannot open global log file: $!\n";
	}
	return $rc;
}

# Arguments:
#   $level - integer: 0 = serious error, 1 = warning...
#   $text  - message text
#
# Side effects:
#   preserve $!
sub logIt($$) {
	my $ec = $!+0;    # Preserve current error code
	my ($level, $text) = (shift, shift);
	my $timeStamp = genTimestamp;

	if ($level <= ${main::Config}->{'verbosity'}) {
		print STDERR $text."\n";
	}

	# Text can be idented on STDERR, but in log it'll always start
	#  with first non-whitespace character.
	$text =~ s/^\s*//;
	my $logText = "$timeStamp ${main::LOGPROGNAME}[$$]: $text\n";
	if ($level <= 1) {
		print LOG $logText     if defined fileno LOG;
	}
	$! = $ec;     # Restore error code
}

# Arguments:
#   $code - program exit code
#   $text - message text to be displayed before exiting
#
# Side effects:
#   aborts program
sub errx($;$) {
	my ($code, $text) = (shift, shift);
	logIt(0, $text) if $text ne '';
	exit $code;
}

1;
